package mk.com.ldm.corepoint.implementation.client.lib.generators;

import java.util.LinkedList;

import mk.com.ldm.corepoint.implementation.shared.LoginData;
import mk.com.ldm.corepoint.implementation.shared.MenuObject;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class MenuGenerator {

	private static MenuGenerator menuGenerator;
	private String activeToken = "";
	private String activeSection = "";
	private String activeLink = "";
	VerticalPanel menu = new VerticalPanel();
	HorizontalPanel headerPanel = new HorizontalPanel();
	FlexTable menuPanel = new FlexTable();
	HorizontalPanel sectionPanel = new HorizontalPanel();
	HorizontalPanel freeLinksPanel = new HorizontalPanel();

	public static MenuGenerator getInstance() {
		if (menuGenerator == null) {
			menuGenerator = new MenuGenerator();
		}
		return menuGenerator;
	}

	protected MenuGenerator() {

	}

	public void create(LinkedList<MenuObject> menuConfig) {
		menuPanel.setStyleName("lt-Menu");
		sectionPanel.setStyleName("lt-Sections");
		int menuCount = 0;

		// dummy object, to render the last object of the menu
		menuConfig.add(new MenuObject("branch", "", "",""));
		// !!

		String currentBranch = "";
		HorizontalPanel currentSection = new HorizontalPanel();

		DOM.getElementById("header").addClassName("lt-Header");
		Button logout = new Button("logout", new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				doLogout();

			}

		});
		logout.setStyleName("lt-Header-logout");
		Label title = new Label(menuConfig.get(0).getName());
		title.setStyleName("lt-Header-title");
		Label placeholder = new Label();
		placeholder.setWidth("680px");
		Label username = new Label(Cookies.getCookie("username"));
		username.setStyleName("lt-Header-user");
		headerPanel.add(title);
		headerPanel.add(placeholder);
		headerPanel.add(username);
		headerPanel.add(logout);
		RootPanel.get("header").add(headerPanel);

		for (final MenuObject m : menuConfig) {
			if (m.getType().equals("branch") || m.getType().equals("item")) {
				if (!currentBranch.isEmpty() && m.getType().equals("branch")) {
					currentSection.getElement().setId(currentBranch + "-root");
					currentSection.addStyleName("lt-Sections-hidden");
					sectionPanel.add(currentSection);
					currentSection = new HorizontalPanel();
					currentBranch = m.getLink();
				} else if (m.getType().equals("branch")) {
					currentBranch = m.getLink();
				}
				
				Hyperlink link;
				if(m.getIcon().isEmpty()){
					 link = new Hyperlink(m.getName(), true, m.getLink());
				}
				else{
					link = new Hyperlink("<img src='/images/"+m.getIcon()+"' /><label>"+m.getName()+"</label>", true, m.getLink());
				}
				if (!m.getName().isEmpty()) {
					menuPanel.setWidget(0, menuCount, link);
					menuPanel.getFlexCellFormatter().getElement(0, menuCount)
							.setId(m.getLink()+"-main");
					menuCount++;
				}

			} else if (m.getType().equals("child")) {
				Hyperlink sectionLink;
				if(m.getIcon().isEmpty()){
					 sectionLink = new Hyperlink(m.getName(), true, m.getLink());
				}
				else{
					sectionLink = new Hyperlink("<img src='/images/"+m.getIcon()+"' /><label>"+m.getName()+"</label>", true, m.getLink());
				}
				sectionLink.getElement().setId(m.getLink() + "-section");
				currentSection.add(sectionLink);
			}
		}

		freeLinksPanel.addStyleName("lt-Sections");
		freeLinksPanel.addStyleName("lt-Sections-hidden");
		freeLinksPanel.getElement().setId("freeLP");

		menu.add(menuPanel);
		menu.add(sectionPanel);
		menu.add(freeLinksPanel);

		RootPanel.get("menu").add(menu);
		changeSection(History.getToken());
	}

	public void changeSection(String token) {

//		if (DOM.getElementById("freeLP") != null)
//			DOM.getElementById("freeLP").addClassName("lt-Sections-hidden");

		if (DOM.getElementById(token.split("/")[0] + "/Form-free") != null)
			DOM.getElementById(token.split("/")[0] + "/Form-free")
					.addClassName("lt-Sections-hidden");

		if (DOM.getElementById(token+"-main") != null) {
			if (DOM.getElementById(activeToken+"-main") != null)
				DOM.getElementById(activeToken+"-main")
						.removeClassName("lt-Tab-active");
			DOM.getElementById(token+"-main").addClassName("lt-Tab-active");
			activeToken = token;
		}

		if ((DOM.getElementById(activeSection + "-root") != null)
				&& !token.split("/")[0].equals(activeSection.split("/")[0])) {
			DOM.getElementById(activeSection + "-root").addClassName(
					"lt-Sections-hidden");
		}

		if (DOM.getElementById(token + "-section") != null) {
			DOM.getElementById(token + "-section").addClassName(
					"lt-Section-active");
			if (DOM.getElementById(activeLink + "-section") != null)
				DOM.getElementById(activeLink + "-section").removeClassName(
						"lt-Section-active");
			activeLink = token;
		}

		if (DOM.getElementById(token + "-root") != null) {
			DOM.getElementById(token + "-root").removeClassName(
					"lt-Sections-hidden");
			activeSection = token;
		}

//		if ((DOM.getElementById(token.split("/")[0]) + "/Form-free") != null) {
//			if (DOM.getElementById("freeLP") != null)
//				DOM.getElementById("freeLP").removeClassName(
//						"lt-Sections-hidden");
//			DOM.getElementById((DOM.getElementById(token.split("/")[0]) + "/Form-free"))
//					.removeClassName("lt-Sections-hidden");
//			activeLink = token;
//		}

	}

	public void doLogout() {
		LoginData.getInstance().setUsername(null);
		LoginData.getInstance().setHash(null);
		Cookies.removeCookie("username");
		Cookies.removeCookie("hash");
		Window.Location.reload();
	}

}
